using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock;
using System.Diagnostics;
using CodePlex.SpaceBlock.FS;
using CodePlex.Resourceful;
using CodePlex.Resourceful.Amazon.S3;

namespace CodePlex.SpaceBlock.UI.Controls.Explore
{
    public partial class ACLControl : CodePlex.SpaceBlock.UI.Controls.BaseControl
    {

        private ACL _acl;
        private readonly DataGridView2 _grid;
        private ACLPrincipal _selectedPrincipal;
        private string _selectedPermission;
        private readonly ActionManager _actions;



        public ACLControl(ActionManager actions)
        {
            _actions = actions;
            InitializeComponent();
           
            _grid = new DataGridView2();

            UIUtils.FillWith(_gridHolder, _grid);


            _grid.AddColumnText("Principal", 100).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            _grid.AddColumnText("Permission", 100).DefaultCellStyle.BackColor = Color.FromArgb(0xf8, 0xf8, 0xf8);


            _grid.SelectionChanged += delegate
            {
                if (_grid.SelectedRows.Count == 0)
                {
                    _descriptionBox.Text = "";
                    _selectedPermission = null;
                    _selectedPrincipal = null;
                }
                else
                {
                    ACL acl = _grid.Tag as ACL;

                    ACLPrincipal p = _grid.SelectedRows[0].Tag as ACLPrincipal;

                    _descriptionBox.Text = Utils.Join("\r\n", Utils.ConvertAll<string, string>(acl.GetPermissions(p), delegate(string permission)
                    {
                        return string.Format("{0}: {1}", permission, acl.GetDescription(permission));
                    }));


                    _selectedPrincipal = p;
                    _selectedPermission = Convert.ToString(_grid.SelectedRows[0].Cells[2].Value);
                }

            };


            if (actions != null)
            {
                actions.BindTo(this);

                actions.Synchronizing += delegate(object sender, CancelEventArgs e)
                {
                    Control c = ((ContextMenuStrip)sender).SourceControl as Control;

                    if (c != this)
                    {
                        return;
                    }
                    foreach (string action in actions.Actions)
                    {
                        actions.SetVisible(action,action == "EditACL");
                    }
                 
                };
                 
            }

        }

        public ACLPrincipal SelectedPrincipal
        {
            get
            {
                return _selectedPrincipal;
            }
        }

        public string SelectedPermission
        {
            get
            {
                return _selectedPermission;
            }
        }

        public ACL ACL
        {
            get
            {
                return _acl;
            }
            set
            {
                _acl = value;

                this.UpdateData();
            }
        }












        private void UpdateData()
        {

            _grid.SuspendLayout();
            
            _grid.Tag = _acl;

        
            _grid.Rows.Clear();

            if (_acl != null)
            {

                foreach (ACLPrincipal p in _acl.Principals)
                {

                    foreach (string permission in _acl.GetPermissions(p))
                    {
                        _grid.AddRow(p,
                            UIManager.Instance.GetSmallImage(p.IsGroup ? UIConstants.ImageHints.UserGroup : UIConstants.ImageHints.User),
                            p.Name,
                            permission);
                    }
                }
            }

          
            _grid.ResumeLayout();

            if (_actions != null && _acl != null)
            {
                _actions.SetEnabled("EditACL", !_acl.IsReadOnly);
            }

        }

  

       
    }
}

